#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long ll;

const int MAXN = 4e4+10;
const int mod = 39989;
const int mody = 1000000000;

struct func{
	double k,b;int num;
	double operator [] (ll x){return k * x + b;}
	double const operator [] (ll x)const{return k * x + b;}
	friend func fmax(func a,func b,ll x){
		if(a[x] > b[x]) return a;
		if(a[x] == b[x] && a.num < b.num) return a;
		return b;
	}
}f[MAXN<<2];

int n,op;
ll ans;

void deal(ll &x){x = (x+ans-1)%mod+1;}
void dealy(ll &y){y = (y+ans-1)%mody+1;}

void upd(int xl,int xr,func v,int o = 1,int l = 1,int r = mod){
//	cout << xl << xr<<".."<<o<<".."<<l<<"-"<<r<<endl;
	if(v[l] < f[o][l] && v[r] < f[o][r]) return;
	int mid = l+r>>1;
	if(xl == l && xr == r){
		if(!f[o].num) return (void)(f[o] = v);
		double l1 = f[o][l],l2 = v[l],r1 = f[o][r],r2 = v[r];
		if(l2 >= l1 && r2 >= r1) return (void)(f[o] = v);
		
		double crossover = (v.b - f[o].b) / (f[o].k - v.k);
		if(l2 > l1){
			if(crossover <= mid){
				upd(xl,mid,v,o<<1,l,mid);
			}else{
				upd(mid+1,xr,f[o],o<<1|1,mid+1,r);
				f[o] = v;
			}
		}else{
			if(crossover <= mid){
				upd(xl,mid,f[o],o<<1,l,mid);
				f[o] = v;
			}else{
				upd(mid+1,xr,v,o<<1|1,mid+1,r);
			}
		}
		return;
	}
	if(xr <= mid) upd(xl,xr,v,o<<1,l,mid);
	else if(xl > mid) upd(xl,xr,v,o<<1|1,mid+1,r);
	else upd(xl,mid,v,o<<1,l,mid),upd(mid+1,xr,v,o<<1|1,mid+1,r);
}

func getmax(int x,int o = 1,int l = 1,int r = mod){
	if(l == r) return f[o];
	int mid = l+r>>1;
	func ans = f[o];
	if(x <= mid) ans = fmax(ans,getmax(x,o<<1,l,mid),x);
	else ans = fmax(ans,getmax(x,o<<1|1,mid+1,r),x);
	return ans;				  
}

int main (){
	#ifndef ONLINE_JUDGE
	freopen("P4097_1.in","r",stdin);
	freopen("P4097.out","w",stdout);
	#endif
	scanf("%d",&n);
	int insertnum = 0;
	for(int i = 1;i <= n;i++){
		scanf("%d",&op);
		if(op == 0){
			ll k;
			scanf("%lld",&k);
			deal(k);
			ans = getmax(k).num;
			printf("%lld\n",ans);
		}else{
			insertnum++;
			ll x0,x1,y0,y1;
			scanf("%lld %lld %lld %lld",&x0,&y0,&x1,&y1);
			deal(x0);deal(x1);dealy(y0);dealy(y1);
			func ff;
			if(x0 == x1) ff = (func){0,max(y0,y1),insertnum};
			else{
				double k = (double)(y1-y0)/(x1-x0);
				double b = (double)y0 - k * x0;
				ff = (func){k,b,insertnum};
			}
			upd(min(x0,x1),max(x0,x1),ff);
		}
	}
	return 0;
}
